package com.zjq.springsocial.renren.api.impl.json;

import java.io.IOException;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.annotate.JsonDeserialize;

import com.zjq.springsocial.renren.api.Like.LikeCatagory;

@JsonIgnoreProperties(ignoreUnknown = true)
abstract class LikeMixin {

	@JsonCreator
	LikeMixin(
			@JsonProperty("catagory") 
			@JsonDeserialize(using = LikeCatagoryDeserializer.class)
			LikeCatagory catagory,// 喜欢的类型
			@JsonProperty("name") String name// 喜欢的东西
	){}
	
	private static class LikeCatagoryDeserializer extends JsonDeserializer<LikeCatagory> {
		@Override
		public LikeCatagory deserialize(JsonParser jp, DeserializationContext ctxt)
				throws IOException, JsonProcessingException {
			try {
				return LikeCatagory.valueOf(jp.getText().toUpperCase());
			} catch (IllegalArgumentException e) {
				return LikeCatagory.INTEREST;
			}
		}
	}
}
